import 'whatwg-fetch';
import 'custom-event-polyfill';
import 'core-js/stable/promise';
import 'core-js/stable/symbol';
import 'core-js/stable/string/starts-with';
import 'core-js/web/url';

import Vue from 'vue'
import App from '@/components/app/index.vue';

import routes from '@/router/index'
import store from '@/store/index'
import $vFN from "@/vfn"

Vue.config.productionTip = false

import index from './libs/index'
import config from './libs/config.json'
Vue.use(index)



import '@/styles/element-variables.scss'


Vue.prototype.$vFN = $vFN;

// 全局异常报错监控
// Vue.config.errorHandler = (err, vm, info) => {
//     Vue.prototype.$vFN.Tools.errorConsole(err, vm, info);
// }






var router = null;

let instance = null;

// 获取子项目列表
 function getAppslist() {
    // 如果不存在则获取
    if (!Vue.prototype.$AppsProjectList) {
     
        let _data = [{
                title: "装修",
                type: "design",
                name: "main",
                container: "#qkDesign"
            },
            {
                title: "平台",
                type: "admin",
                name: "main",
                container: "#qkAdmin"
            },
            {
                title: "站点",
                type: "site",
                name: "main",
                container: "#qkSite"
            },
            {
                title: "门店系统",
                type: "store",
                name: "main",
                container: "#qkStore"
            },
        ]

        // 合并扩展数据
        let _list2 = Vue.prototype.$AppsExpandConfig;
        // let _list1 = _data.data;
        let _list1 = _data;


        _list2.forEach(item => {
            let _is = 0;
            _list1.forEach(iitem => {
                if (item.name == iitem.name && item.type == iitem.type) {
                    _is += 1;
                    for (let i in item) {
                        iitem[i] = item[i];
                    }
                }
            })
            if (_is == 0) {
                _list1.push(item);
            }
        })
        Vue.prototype.$AppsProjectList = JSON.parse(JSON.stringify(_list1));

        
        return true;
    }
    return true;

}
window.MicroAppConfiguration = {
    config
}

function render({
    data = {},
    container
} = {}) {
    __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
    Vue.prototype.$AppsConfig = data.config;
    Vue.prototype.$Window = data.window;
    Vue.prototype.$AppsExpandConfig = data.expandConfig;


     // 静态资源地址
     Vue.prototype.$STATIC_RESOURCE_DOMAIN =data.config.apiDomain.STATIC_RESOURCE_DOMAIN
     // 用户端访问地址
     Vue.prototype.$USER_SHOP_URL = data.config.apiDomain.USER_SHOP_URL

    router = new VueRouter({
        mode: 'history',
        // base: process.env.BASE_URL,
        routes
    });


    router.beforeEach(async (to, from, next) => {
        getAppslist()
        console.error(222,to)
        if (to.meta.isOpen) {
            // 设置浏览器标题
            // document.title = vFN.Constants.systemName + to.meta.title;
        } else {
            // Tool.menuConfig(to.name);
        }
        //
        // if(to.meta.mixinPage_vueName!="index"){
        //     publicFn.moduleSwitchSelect(to.meta.ancestorAppId);
        //     publicFn.setMenuSelect(to.meta.parentId);
        //   }
        next();

    });
    router.afterEach((to, from) => {
        // console.error("afterEach", from.path,to.path)

    });
    Vue.prototype.$Router = router;

    instance = new Vue({
        router,
        store,
        render: h => h(App),
    }).$mount(container ? container.querySelector('#jnmApp') : '#jnmApp');

}

export async function bootstrap() {
    console.groupEnd();
    console.group("%c 主程序 APP-MAIN 初始化项目", "background:#4569d4; padding: 1px; border-radius:3px; color: #fff;");
    console.groupEnd();
}

export async function mount(props) {
    console.group("%c 主程序 APP-MAIN  挂载项目", "background:#4569d4; padding: 1px; border-radius:3px; color: #fff;");

    render(props);
}

export async function unmount() {
    console.groupEnd();
    console.group("%c 主程序 APP-MAIN  卸载项目", "background:#4569d4; padding: 1px; border-radius:3px; color: #fff;");
    console.groupEnd();
    instance.$destroy();
    instance.$el.innerHTML = "";
    instance = null;
}